﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Handrise.CDCable.Mobile.Business;

namespace Handrise.CDCable.Mobile.UI
{
    public partial class FrmSelectDevice : FrmBase
    {
        private string targetForm = "";
        private object parentInfo;
        private List<Device> devices;
      
        public FrmSelectDevice()
        {
            InitializeComponent();
            ttlbrhead.Text = "设备选择";
            ilstbcup.Images.Add(ResourceImage.空白);
            ilstdown.Images.Add(ResourceImage.空白);
            ilstbcup.Images.Add(ResourceImage.空白);
            ilstdown.Images.Add(ResourceImage.空白);
            ilstbcup.Images.Add(ResourceImage.返回01);
            ilstdown.Images.Add(ResourceImage.返回02);
            btnPrior.UpImage = ResourceImage.上一页可点击;
            btnPrior.DownImage = ResourceImage.上一页点击;
            btnPrior.DisableImage = ResourceImage.上一页灰;
            btnNext.UpImage = ResourceImage.下一页可点击;
            btnNext.DownImage = ResourceImage.下一页点击;
            btnNext.DisableImage = ResourceImage.下一页灰;
        }

        private void FrmSelectDevice_Load(object sender, EventArgs e)
        {
            if (IsReturn)
                LoadCacheData();
            else
                LoadData();

            dlDevices.DisplayMember = "Name";
            dlDevices.DataSource = devices;
            dlDevices.DataBind();

            SetButtonState();
        }

        /// <summary>
        /// 加载数据
        /// </summary>
        private void LoadData()
        {
            parentInfo = Parameter[0];
            object[] parentParameters = (object[])parentInfo;
            targetForm = (string)parentParameters[0];
            devices = (List<Device>)Parameter[1];
        }

        /// <summary>
        /// 加载缓存数据
        /// </summary>
        private void LoadCacheData()
        {
           parentInfo = Caches[0];
           object[] parentParameters = (object[])parentInfo;
           targetForm = (string)parentParameters[0];
           devices = (List<Device>)Caches[1];
        }

        private void dlDevices_OnItemClick(HandRise.MBHOP.Controls.DataListItem item)
        {
            Device device = (Device)item.DataSource;
            FormManage.GotoNext(targetForm, new object[] {device, parentInfo }, new object[] {parentInfo, devices }, false);

        }

        private void bcOperate_OnButtonClick(object sender, int buttonIndex)
        {
            switch (buttonIndex)
            {
                case 2:
                    FormManage.GotoNext("FrmQueryDevice", null, null, true);
                    break;
            }
        }

        private void SetButtonState()
        {
            btnNext.Enabled = dlDevices.PageCount > 0 && dlDevices.PageIndex < dlDevices.PageCount;
            btnPrior.Enabled = dlDevices.PageCount > 0 && dlDevices.PageIndex > 1;
            lblPageInfo.Text = dlDevices.PageIndex.ToString() + "/" + dlDevices.PageCount.ToString();
        }

        private void btnPrior_Click(object sender, EventArgs e)
        {
            dlDevices.PreviousPage();
            SetButtonState();
        }

        private void btnNext_Click(object sender, EventArgs e)
        {
            dlDevices.NextPage();
            SetButtonState();


        }



    }
}